<?php

namespace App;

use DB;
use Illuminate\Database\Eloquent\Model;
use Storage;

/**
 * Class Banner
 *
 * @package App
 * @mixin \Eloquent
 */
class Ztfenlei extends Model
{
    public $timestamps = false;
    protected $table = 'kl_tag_dic';

    //是否显示
    const DEL_OFF = 1;
    const DEL_ON = 0; 

    static $DelDisplay = [
        self::DEL_OFF => '否',
        self::DEL_ON => '是'
    ];

    //是否在首页显示
    const SHOW_OFF = 0;
    const SHOW_ON = 1;
    static $ShowDisplay = [
        self::SHOW_OFF => '不显示',
        self::SHOW_ON => '显示',
    ];
 
    /**
     * 添加专题
     * @param $request
     * @return array
     */
    static function ZtfenleiPublish($request)
    {
        $fenlei = new Ztfenlei();
        //fenlei
        if ($request->has('pic')) {
            $path = Storage::url(Postings::OssUpdate($request->pic));
            $fenlei->pic = $path;
        } else {
            return ['status' => 2, 'message' => '图片呢?'];
        }
        $fenlei->name = $request->name;
		$fenlei->remark = $request->remark;
		$fenlei->is_homepage = $request->is_homepage;
		$fenlei->is_del = $request->is_del;
		$fenlei->create_time = date("Y-m-d H:i:s");
        if (!$fenlei->save()) {
            return ['status' => 2, 'message' => '添加失败'];
        }

        return ['status' => 1, 'message' => '添加成功'];
    }


    /**
     * 修改专题
     * @param $request
     * @param $Ztfenlei
     * @return array
     */
    static function ZtfenleiModify($request, $Ztfenlei)
    {

        //Ztfenlei
        if ($request->has('pic')) {
            $path = Storage::url(Postings::OssUpdate($request->pic));
            $Ztfenlei->pic = $path;
        }

        $Ztfenlei->name = $request->name;
        $Ztfenlei->remark = $request->remark;
		$Ztfenlei->is_homepage = $request->is_homepage;
		$Ztfenlei->is_del = $request->is_del;  
		

        if (!$Ztfenlei->save()) {
            return ['status' => 2, 'message' => '修改失败'];
        }

        return ['status' => 1, 'message' => '修改成功'];
    }
	
	//删除工具
    static function Del($id, $request)
    {
        DB::beginTransaction();

        if (!Ztfenlei::where('id', $id)->update([
            'is_del' => Ztfenlei::DEL_ON
        ])) {
            DB::rollBack();

            return ['status' => 2, 'message' => '删除失败'];
        }

        DB::commit();

        return ['status' => 1, 'message' => '删除成功'];
    }
	
		
	//刷新
	static function Shuaxin($id, $request){
		DB::beginTransaction();

        if (!Ztfenlei::where('id', $id)->update([
            'create_time' => date('Y-m-d H:i:s',time())
        ])) {
            DB::rollBack();

            return ['status' => 2, 'message' => '刷新失败'];
        }

        DB::commit();

        return ['status' => 1, 'message' => '刷新成功'];
	}

}
